package com.haofengsoft.scce.adapter.information;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.ui.consultation.ConsultationDetailActivity;

public class ZhongNanAdapter extends BaseAdapter {

	private Context context;
	private List<ZhongNanModel> zhongNanModelList;
	private LayoutInflater inflater = null;

	public ZhongNanAdapter(Context context, List<ZhongNanModel> zhongNanModelList) {
		this.context = context;
		this.zhongNanModelList = zhongNanModelList;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return this.zhongNanModelList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.zhongNanModelList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void addZhongNanList(List<ZhongNanModel> zhongNanModelList) {
		this.zhongNanModelList.addAll(zhongNanModelList);
		// notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = inflater.inflate(R.layout.consultation_zhong_nan_item, null);
		TextView titleTv = (TextView) view.findViewById(R.id.zhong_nan_item_title_tv);
		TextView detailTv = (TextView) view.findViewById(R.id.zhong_nan_item_detail_tv);
		TextView timeTv = (TextView) view.findViewById(R.id.zhong_nan_item_time_tv);
		RelativeLayout itemRl = (RelativeLayout) view.findViewById(R.id.info_zhong_nan_item_rl);
		ZhongNanModel zhongNanModel = (ZhongNanModel) getItem(position);
		titleTv.setText(zhongNanModel.getTitle());
		detailTv.setText(zhongNanModel.getDetail());
		timeTv.setText(zhongNanModel.getTime());
		itemRl.setOnClickListener(new ItemClickListener(zhongNanModel));
		return view;
	}

	class ItemClickListener implements OnClickListener {

		ZhongNanModel zhongNanModel;

		public ItemClickListener(ZhongNanModel zhongNanModel) {
			this.zhongNanModel = zhongNanModel;
		}

		@Override
		public void onClick(View view) {
			Intent consultationDetailIntent = new Intent(context, ConsultationDetailActivity.class);
			String sourceUrl = ConstantConfig.API_URL + ConstantConfig.INFO_CONNECT_WEB_URL + zhongNanModel.getId();
			consultationDetailIntent.putExtra("sourceUrl", sourceUrl);
			consultationDetailIntent.putExtra("title", zhongNanModel.getTitle());
			consultationDetailIntent.putExtra("time", zhongNanModel.getTime());
			consultationDetailIntent.putExtra("source", zhongNanModel.getSoure());
			consultationDetailIntent.putExtra("newsId", zhongNanModel.getId());
			consultationDetailIntent.putExtra("general", zhongNanModel.getDetail());
			context.startActivity(consultationDetailIntent);
		}

	}

	static class ViewHolder {
		TextView titleTv;
		TextView detailTv;
		TextView timeTv;
	}
}
